package com.utilities.regex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class HttpParamParser {

    private static Matcher compileAndMatcher(String input, String regex) {
        Pattern compile = Pattern.compile(regex);
        return compile.matcher(input);
    }

    public static Map<String, List<String>> resolveParam(String input){
        Map<String,List<String>> ans=new HashMap<>();
        String  regex="(?:^| |&|\\?)(\\w+)=([^&]*)";
        Matcher matcher = compileAndMatcher(input, regex);
        while (matcher.find()){
            String name = matcher.group(1),value=matcher.group(2);
            ans.computeIfAbsent(name,k->new ArrayList<>()).add(value);
        }
        return ans;
    }

    public static void main(String[] args) {

        System.out.println(resolveParam("b=2&c=3&c=5"));
    }
}
